﻿using CMS.ViewModel;
using LiveCharts;
using LiveCharts.Defaults;
using LiveCharts.Wpf;
using Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CMS.View
{
    /// <summary>
    /// IndexView.xaml 的交互逻辑
    /// </summary>
    public partial class IndexView : UserControl
    {
        public IndexView()
        {
            InitializeComponent();
            Loaded += (s, e) =>
            {
                var vm = DataContext as IndexViewModel;
                var cargoProvider = new CargoProvider();
                var recordProvider=new RecordProvider();
                var cargos = cargoProvider.Select();
                var recods= recordProvider.Select();
                vm.CargoLabels.Clear();
                vm.CargoChartValues.Clear();
                vm.PieSeries.Clear();
                //显示物资总量报表
                cargos.ForEach(cargo =>
                {
                    var list = recods.FindAll(item => item.CargoId == cargo.Id);
                    if (list != null)
                    {
                        var inputCount = list.FindAll(item => item.RecoredType == true).Sum(item => item.Number);
                        var outputCount = list.FindAll(item => item.RecoredType == false).Sum(item => item.Number);
                        var sumCount = inputCount - outputCount;
                        cargo.Sum = sumCount;
                    }
                    vm.CargoLabels.Add(cargo.Name);
                    vm.CargoChartValues.Add(cargo.Sum);
                    vm.PieSeries.Add(new PieSeries() 
                    { Title = cargo.Name,
                        Values = new ChartValues<double>() { cargo.Sum} 
                    });
                });
                //显示用户流水报表
                var memberProvider = new MemberProvider();
                var members=memberProvider.Select();
                vm.MemberLabels.Clear();
                vm.MenberChartValues.Clear();
                members.ForEach(member =>
                {
                    int count = recods.FindAll(recod => recod.MenberId == member.Id).Count;
                    vm.MemberLabels.Add(member.Name);
                    vm.MenberChartValues.Add(count);
                });
              
                foreach (var cargo in cargos)
                {
                    var list = recods.FindAll(record => record.CargoId == cargo.Id);
                    var serise = new LineSeries();
                    serise.Title = cargo.Name;
                    serise.Values = new ChartValues<double>();
                    Axis axis = new Axis();
                    axis.ShowLabels = true;
                    axis.Labels = new List<string>();

                    list.ForEach(record =>
                    {
                        axis.Labels.Add(record.InserDate.ToString());
                        serise.Values.Add(record.Number);
                    });
                    vm.LineAxis.Add(axis);
                    vm.LineSeries.Add(serise);
                 
                    break;
                }


                return;
                //显示每个物资的曲线图
                cargos.ForEach(cargo =>
                {
                    var list = recods.FindAll(item => item.CargoId == cargo.Id);
                    var serise = new LineSeries();
                    serise.Title = cargo.Name;
                    serise.Values = new ChartValues<double>();
                   Axis axis= new Axis();
                    axis.ShowLabels= true;
                    axis.Labels=new List<string>();
                    
                    list.ForEach(record =>
                    {
                        axis.Labels.Add(cargo.InserDate.ToString());
                        serise.Values.Add(record.Number);
                       
                    });
                    vm.LineSeries.Add(serise);
                    vm.LineAxis.Add(axis);
                });

               
            };
        }
    }
}
